const app = getApp()
const cookieUtil = require("../../utils/cookie.js")

Page({
  data: {},
  onLoad(options) {
	  //获得这是哪个活动
	  const locationId = options.locationId
	  const title = options.name
	  my.setNavigationBar({
		  title,
	  })
	  let url = app.globalData.baseUrl + app.globalData.clubUrl
	  let data = {}
	  data.locationId = locationId
	  let promise = new Promise((resolve, reject) => {
	  	my.request({
	  		url,
			data,
	  		method: "GET",
	  		success: (res) => {
	  			resolve(res)
	  		}
	  	})
	  })
	  promise.then((res) => {
		  let user_like_club_list = []
		  if (res.data.length) {
		  	user_like_club_list = res.data
		  	// user_like_club_list.map((obj) => obj["likeStatus"] = false)
		  	this.setData({
		  		user_like_club_list
		  	})
		  }
	  })
	  
	  //判断有无登录
	  url = app.globalData.baseUrl + app.globalData.registerUrl
	  //从缓存中拿取cookie
	  const cookie = cookieUtil.getCookieFromStorage().data
	  if (!cookie) {
	  	app.globalData.userStatus = false
	  } else {
	  	let promise = new Promise((resolve, reject) => {
	  		
	  		let headers = {}
	  		headers.cookie = cookie
	  		
	  		my.request({
	  			url,
	  			headers,
	  			method: "GET",
	  			success: (res) => {
	  				resolve(res)
	  			}
	  		})
	  	})
	  	
	  	promise.then((res) => {
	  		//如果未登录则进行跳转
	  		if (res.data.status == "fail") {
	  			//需要把缓存清空
	  			cookieUtil.setCookieToStorage()
	  			app.globalData.userStatus = false
	  		} else {
	  			app.globalData.userStatus = true
	  		}			
	  	})
	  }
	  console.log(options)
  },
  OnOrder (e) {
  	//获得活动对应下标
	if (!app.globalData.userStatus) {
		let promise = new Promise((resolve, reject) => {
			my.confirm({
				 title: '温馨提示',
				 content: '您还没有注册/登录哦',
				 confirmButtonText: '登录注册',
				 cancelButtonText: '我再看看',
				 success: (result) => {
					if (result.confirm) {
						my.navigateTo({
							url:"/pages/register/register"
						})
					}
				 },
			})
		})
	} else {
		const clubId = e.currentTarget.dataset.id//这个活动的id
		const idx = e.currentTarget.dataset.idx//列表的索引
		//封装数据
		const url = app.globalData.baseUrl + app.globalData.clublikestatusUrl
		const cookie = cookieUtil.getCookieFromStorage().data
		let headers = {}
		let data = {}
		headers.cookie = cookie
		data.clubId = clubId
		my.request({
			data,
			url,
			headers,
			method: "GET",
			success: (res) => {
				console.log(res)
				my.showToast({
					type: 'success',
					content: res.data.message,
					duration: 2000,
				})
				//图标状态
				let user_like_club_list = this.data.user_like_club_list
				if (res.data.message.search("取消") != -1) {
					user_like_club_list[idx]["likeStatus"] = false
				} else {
					user_like_club_list[idx]["likeStatus"] = true
				}
				this.setData({
					user_like_club_list
				})
			}
		})
	}
  	 	
  },
});
